<?php

class Database
{
	public $connection;
	/**
	 *Connect to the minipool database
	 */
	public function connect()
	{
		// Create connection
		$this->connection = mysqli_connect("localhost","root","","minipool");

		// Check connection
		if (mysqli_connect_errno($this->connection))
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
		//else
		//echo "Connected to database!";
	}

	public function disconnect()
	{
		mysqli_close($this->connection);
	}
	
	public function getConflicts($id)
	{
		$this->connect();
		
		$query = "SELECT * FROM Trip WHERE id <> '$id'";

		$result = mysqli_query($this->connection, $query)  or die(mysql_error());

		$returner = array();
		while( $row = mysqli_fetch_array($result) )
			$returner[] = $row;
		
		$this->disconnect();

		return $returner;
	}
	
	public function getTrip($id)
	{
		$this->connect();

		$query = "SELECT * FROM Trip WHERE id='$id'";

		$result = mysqli_query($this->connection, $query)  or die(mysql_error());

		$row = mysqli_fetch_array($result);
		
		$this->disconnect();

		return $row;
	}

	public function getTrips($reg)
	{
		$this->connect();

		$query = "SELECT * FROM Trip WHERE vehicle_reg='$reg'";

		$result = mysqli_query($this->connection, $query)  or die(mysql_error());

		$returner = array();
		while( $row = mysqli_fetch_array($result) )
			$returner[] = $row;
		
		$this->disconnect();

		return $returner;
	}


	public function getVehicles($name)
	{
		if( gettype($name) == "integer" ) $id = $name;
		else $id = $this->companyIdForName($name);
		
		$this->connect();

		$query = "SELECT * FROM Vehicle WHERE company_id='$id'";
		
		$result = mysqli_query($this->connection, $query)  or die(mysql_error());
		
		$returner = array();
		while( $row = mysqli_fetch_array($result) )
			$returner[] = $row;

		$this->disconnect();

		return $returner;
	}

	public function companyIdForName($name)
	{
		$this->connect();

		$query = "SELECT id FROM Company WHERE name='".$name."'";

		$result = mysqli_query($this->connection, $query)  or die(mysql_error());

		$row = mysqli_fetch_array($result);

		$this->disconnect();

		return $row['id'];
	}

	public function addCompany($name, $email, $phone)
	{
		$this->connect();

		$query = "INSERT INTO Company (name, phone, email) VALUES ('$mysqli->real_escape_string($name)','$mysqli->real_escape_string($phone)','$mysqli->real_escape_string($email)'";

		if (!mysqli_query($this->connection, $query))
		{
			die('Error: ' . mysqli_error($this->connection));
		}
		
		$this->disconnect();
	}
	
	public function addVehicle($reg, $capacity, $wc, $company_id)
	{
		$this->connect();

		$query = "INSERT INTO Vehicle (registration, capacity, wheelchair_capacity, company_id)".
		" VALUES ('$reg','$capacity','$wc','$company_id')";
		
		if (!mysqli_query($this->connection, $query))
		{
			die('Error: ' . mysqli_error($this->connection));
		}
		
		return true;
		
		$this->disconnect();
	}
	
	public function addTrip($org, $dest, $date, $driver, $vehicle_reg)
	{
		$this->connect();

		$query = "INSERT INTO Trip (origin, destination, driver, date, vehicle_reg, seats_used)".
		" VALUES ('$org','$dest','$driver','$date','$vehicle_reg',0)";
		
		echo $query;
		
		if (!mysqli_query($this->connection, $query))
		{
			die('Error: ' . mysqli_error($this->connection));
		}
		
		$this->disconnect();
	}

}


